import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
// import User from '../components/User/user'
import Authority from '../components/User/Authority'

Vue.use(Router)

 const router = new Router({
  routes: []
})

let getRouter = [{
  path:'/welcome',
  component:Welcome,
  },
  {
    path:'/user',
    component:resolve => require(['../components/User/user'],resolve),
  },
  {
    path:'/rights',
    component:resolve => require(['../components/power/Rights'],resolve),
  },
  {
    path:'/roles',
    component:resolve => require(['../components/power/Roles'],resolve),
  }
]
router.addRoutes([
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:getRouter
  }
])

// 挂载路由导航守卫
router.beforeEach((to, from, next)=>{
  if(to.path === '/login') return next()

  // 获取token
  const token = window.sessionStorage.getItem('token')
  console.log(token)
  if(!token){
    next('/login')
  }else{
    next()
  }
  
})
export default router
